package top.xcxdl.rural.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import top.xcxdl.rural.entity.RuralArea;
import top.xcxdl.rural.entity.RuralTab;
import top.xcxdl.rural.mapper.RuralAreaMapper;
import top.xcxdl.rural.service.IRuralAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.xcxdl.rural.utils.ImageAddress;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-05-17
 */
@Service
public class RuralAreaServiceImpl extends ServiceImpl<RuralAreaMapper, RuralArea> implements IRuralAreaService {

    @Override
    public PageInfo<RuralArea> getList(Integer pageNo, Integer pageSize, String key) {
        PageHelper.startPage(pageNo, pageSize);
        List<RuralArea> list = baseMapper.getList(key);
        list.forEach(ruralArea -> {
            // 将字符串转换为数组-再转为List
            ruralArea.setAreaImagesArray(ruralArea.getAreaImagesString() == null ? new ArrayList<>() : Arrays.asList(ruralArea.getAreaImagesString().split(",")));
            ruralArea.setAreaVideosArray(ruralArea.getAreaVideosString() == null ? new ArrayList<>() : Arrays.asList(ruralArea.getAreaVideosString().split(",")));
            ruralArea.setAreaImagesString(null);
            ruralArea.setAreaVideosString(null);
            // 遍历list，将图片、视频路径转换为绝对路径
            List<String> areaImagesArrayTemp = new ArrayList<>();
            List<String> areaVideosArrayTemp = new ArrayList<>();
            ruralArea.getAreaImagesArray().forEach(imagePath -> {
                areaImagesArrayTemp.add(ImageAddress.getAllPath(imagePath));
            });
            ruralArea.getAreaVideosArray().forEach(videoPath -> {
                areaVideosArrayTemp.add(ImageAddress.getAllPath(videoPath));
            });
            ruralArea.setAreaImagesArray(areaImagesArrayTemp);
            ruralArea.setAreaVideosArray(areaVideosArrayTemp);
        });
        return new PageInfo<>(list);
    }

    @Override
    public int doEdit(RuralArea area) {
        area.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        if (area.getAreaId() == null) {
            QueryWrapper<RuralArea> wrapper = new QueryWrapper<>();
            wrapper.eq("adcode", area.getAdcode());
            int count = baseMapper.selectCount(wrapper);
            if (count > 0) {
                // adcode已存在
                return -1;
            }
            area.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
            return baseMapper.insert(area);
        } else {
            return baseMapper.updateById(area);
        }
    }

    @Override
    public int delete(List<Integer> ids) {
        QueryWrapper<RuralArea> wrapper = new QueryWrapper<>();
        wrapper.in("area_id", ids);
        return baseMapper.delete(wrapper);
    }

    @Override
    public RuralArea getByAdCode(String adCode) {
        RuralArea ruralArea = baseMapper.getByAdCode(adCode);
        if (ruralArea != null) {
            // 将字符串转换为数组-再转为List
            ruralArea.setAreaImagesArray(ruralArea.getAreaImagesString() == null ? new ArrayList<>() : Arrays.asList(ruralArea.getAreaImagesString().split(",")));
            ruralArea.setAreaVideosArray(ruralArea.getAreaVideosString() == null ? new ArrayList<>() : Arrays.asList(ruralArea.getAreaVideosString().split(",")));
            ruralArea.setAreaImagesString(null);
            ruralArea.setAreaVideosString(null);
            // 遍历list，将图片、视频路径转换为绝对路径
            List<String> areaImagesArrayTemp = new ArrayList<>();
            List<String> areaVideosArrayTemp = new ArrayList<>();
            ruralArea.getAreaImagesArray().forEach(imagePath -> {
                areaImagesArrayTemp.add(ImageAddress.getAllPath(imagePath));
            });
            ruralArea.getAreaVideosArray().forEach(videoPath -> {
                areaVideosArrayTemp.add(ImageAddress.getAllPath(videoPath));
            });
            ruralArea.setAreaImagesArray(areaImagesArrayTemp);
            ruralArea.setAreaVideosArray(areaVideosArrayTemp);
        }
        return ruralArea;
    }
}
